using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AFI.Speargun.Web.Models;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data.Objects.DataClasses;
using AFI.Speargun.Web.Helpers;
using AFI.Speargun.Web.Models.Validation;
using AFI.Speargun.Web.Core;
using AFI.Speargun.Web.Paging;

namespace AFI.Speargun.Web.Controllers
{
    public class DepartmentController : BaseController
    {
        //
        // GET: /Department/AjaxList

        [Authorize]public ActionResult AjaxList()
        {
            this.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Department root = _service.GetDepartment(Guid.Empty);
            string json = root.ToJson<Department>();

            return Content(json,
            "text/x-json",
            System.Text.Encoding.UTF8);
        }

        //
        // GET: /Department/{id}?page={page}

        [Authorize]public ActionResult Index(Guid? id, int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;

            Department department = _service.GetDepartment(id);

            ViewData["Appellation"] = department.Appellation;
            ViewData["DepartmentId"] = department.DepartmentId;

            if (!department.AssetEntry.IsLoaded)
                department.AssetEntry.Load();

            var assetEntries = department.AssetEntry.ToPagedList(currentPageIndex, pageSize);

            return View(assetEntries);
        }

        //
        // GET: /Department/Create/{id}

        [Authorize]public ActionResult Create(Guid id)
        {
            return View(_service.GetDepartment(id));
        }

        //
        // POST: /Department/Create/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]public ActionResult Create(Guid id, [Bind(Exclude = "DepartmentId")] Department department)
        {
            if (_service.CreateDepartment(id, department))
                return RedirectToAction("Index", new { id = department.DepartmentId });
            return View();
        }

        //
        // POST: /Department/AjaxCreate

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]public ActionResult AjaxCreate(Guid parentDepartmentId)
        {
            int returnValue = 0;
            string returnMessage = String.Empty;

            Department department = new Department()
            {
                Appellation = "New Department",
            };

            if (!_service.CreateDepartment(parentDepartmentId, department))
            {
                returnValue = 1;
                returnMessage = ModelState.GetValidationSummary();
            }

            JObject returnJson =
                new JObject(
                    new JProperty("returnValue", returnValue),
                    new JProperty("returnMessage", returnMessage)
                );

            return Content(returnJson.ToString(),
                "text/x-json",
                System.Text.Encoding.UTF8);
        }

        //
        // GET: /Department/Edit/{id}

        [Authorize]public ActionResult Edit(Guid id)
        {
            return View(_service.GetDepartment(id));
        }

        //
        // POST: /Department/Edit/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]public ActionResult Edit(Guid id, Department department)
        {
            if (_service.EditDepartment(department))
                return RedirectToAction("Index", new { id = department.DepartmentId });
            return View(department);
        }

        //
        // POST: /Department/AjaxRename

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]public ActionResult AjaxRename(Guid departmentId, string appellation)
        {
            int returnValue = 0;
            string returnMessage = String.Empty;

            var department = _service.GetDepartment(departmentId);
            department.Appellation = appellation;

            if (!_service.EditDepartment(department))
            {
                returnValue = 1;
                returnMessage = ModelState.GetValidationSummary();
            }

            JObject returnJson =
                new JObject(
                    new JProperty("returnValue", returnValue),
                    new JProperty("returnMessage", returnMessage)
                );

            return Content(returnJson.ToString(),
                "text/x-json",
                System.Text.Encoding.UTF8);
        }

        //
        // GET: /Categroy/Delete/{id}

        [Authorize]public ActionResult Delete(Guid id)
        {
            return View(_service.GetDepartment(id));
        }

        //
        // POST: /Department/Delete/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]public ActionResult Delete(Guid id, Department department)
        {
            if (_service.DeleteDepartment(id))
                return RedirectToAction("Index");
            return View(department);
        }

        //
        // POST: /Department/AjaxDelete

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]public ActionResult AjaxDelete(Guid departmentId)
        {
            int returnValue = 0;
            string returnMessage = String.Empty;

            if (!_service.DeleteDepartment(departmentId))
            {
                returnValue = 1;
                returnMessage = ModelState.GetValidationSummary();
            }

            JObject returnJson =
                new JObject(
                    new JProperty("returnValue", returnValue),
                    new JProperty("returnMessage", returnMessage)
                );

            return Content(returnJson.ToString(),
                "text/x-json",
                System.Text.Encoding.UTF8);
        }
    }
}
